import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Router } from '@angular/router';

import { NoticeService } from '../service/notice.service';
import { Message } from '../model/message.model';

import 'rxjs/add/operator/toPromise';

@Injectable()
export class HttpService {

    public static SUCCESS = 0;
    public static CODE_200 = '';
    public static CODE_401 = '';
    public static CODE_500 = '系统错误,请稍候再试';
    public static CODE_DEFAULT = '网络异常，请稍候再试';

    constructor(
        public http: HttpClient,
        public router: Router,
        public noticeService: NoticeService) { }

    public httpGet(url: string) {
        this.http.get(url).toPromise()
            .then(response => {
                this.success(response);
            }).catch(error => {
                this.error(error);
            });
    }

    public httpPost(url: string, body: any = null) {
        this.http.post(url, body).toPromise()
            .then(response => {
                this.success(response);
            }).catch(error => {
                this.error(error);
            });
    }

    public httpGetSuccess(url: string, success: any = null) {
        this.http.get(url).toPromise()
            .then(response => {
                this.runSuccessCallback(response, success);
            }).catch(error => {
                this.error(error);
            });
    }

    public httpPostSuccess(url: string, body: any = null, success: any = null) {
        this.http.post(url, body).toPromise()
            .then(response => {
                this.runSuccessCallback(response, success);
            }).catch(error => {
                this.error(error);
            });
    }

    public httpGetCallback(url: string, callback: any) {
        this.http.get(url).toPromise()
            .then(response => {
                this.runCallback(response, callback);
            }).catch(error => {
                this.error(error);
            });
    }

    public httpPostCallback(url: string, body: any = null, callback: any) {
        this.http.post(url, body).toPromise()
            .then(response => {
                this.runCallback(response, callback);
            }).catch(error => {
                this.error(error);
            });
    }

    private error(error) {
        let code = error.status;
        if (!code) {
            return;
        }
        console.log('HttpService http request error:', error);
        switch (code) {
            case 401:
                // 无权限直接到登录页面
                this.router.navigate(['/login', 1]);
                break;
            case 500:
                this.noticeService.error(HttpService.CODE_500);
                break;
            default:
                this.noticeService.error(HttpService.CODE_DEFAULT);
        }
    }

    private success(response) {
        let msg = response as Message;
        if (msg.errorCode === HttpService.SUCCESS) {
            this.noticeService.success(msg.data);
        } else {
            console.log('HttpService http response:', response);
            this.noticeService.error(HttpService.CODE_500);
        }
    }

    private runSuccessCallback(response: any, callback: any) {
        let msg = response as Message;
        if (msg.errorCode === HttpService.SUCCESS && typeof callback === 'function') {
            callback(response);
        }
    }

    private runCallback(response: any, callback: any) {
        if (typeof callback === 'function') {
            callback(response);
        }
    }

}
